#include "Components/GraphicsComponent.hpp"
#include "Components/IComponent.hpp"
//#include "Components/ICompositor.hpp"
#include "Components/KineticsComponent.hpp"
#include "Components/Message.hpp"
#include "Math/Vector.hpp"
#include "Utility/InterfaceConversion.hpp"
#include "Utility/Singleton.hpp"

#include "SFML/Graphics/RenderWindow.hpp"
#include "SFML/Graphics/Sprite.hpp"

namespace Components
{
	GraphicsComponent::GraphicsComponent( /*ICompositor &comp,*/ KineticsComponent &kin )
		: /*compositor( comp ),*/ kinetics( kin ), renderWnd( *Utility::Singleton< sf::RenderWindow >::Get() )
	{
		sprite.SetCenter( 0.5f, 0.5f );
	}

	GraphicsComponent::~GraphicsComponent( void )
	{
	}

	void GraphicsComponent::Update( void )
	{
		if ( visible )
		{
			renderWnd.Draw( sprite );
		}
	}

	void GraphicsComponent::RecieveMessage( const Message &msg )
	{
		if ( msg.GetHeader() == "Kinetics:Update" )
		{
			sprite.SetPosition( Utility::SFMLVector< float >( kinetics.GetPosition() ) );
			sprite.SetRotation( kinetics.GetAngle() );
		}
	}

	void GraphicsComponent::SetColour( const Graphics::Colour &colour )
	{
		sprite.SetColor( Utility::SFMLColour( colour ) );
	}

	void GraphicsComponent::SetSize( const Math::Vectorf &size )
	{
		sprite.Resize( Utility::SFMLVector( size, false ) );
	}

	void GraphicsComponent::SetTexture( const Graphics::Texture &tex )
	{
		sf::Vector2f size = sprite.GetSize();
		sprite.SetImage( tex.img );
		sprite.Resize( size );
		sprite.SetCenter( Utility::SFMLVector< float >( tex.GetSize() / 2.0f, false ) );
	}

	void GraphicsComponent::SetVisible( const bool setVis )
	{
		visible = setVis;
	}
}